package Controllers;

import java.awt.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Models.Project.*;
/**
 * Servlet implementation class postProject
 */
public class ProjectController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArrayList<String> paramType = new ArrayList<String>();
	private ArrayList param = new ArrayList();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProjectController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private String getXML(Project p,String pram) throws SQLException
	{
		String xml="";
		
		if(pram.equals("ProjectNameId"))
		{
			ArrayList<Project> AdvisorPostedProjects= p.viewproject();
			xml+="<Projects>";
			int size=AdvisorPostedProjects.size();
			
			for  (int i=1 ; i<size ; i++)
				{
					Project proj=AdvisorPostedProjects.get(i);
					xml+="<Project><Name>"+ proj.getProjectName()+"</Name><Id>"+proj.getProjectId()+" </Id></Project>";
				}
			xml+="</Projects>";
		}
		else if(pram.equals("projectdetail"))
		{
			xml+="<Project>";
			xml+="<PrjectId>"+p.getProjectId();
			xml+="</PrjectId><PrjectName>"+p.getProjectName();
			xml+="</PrjectName><PrjectDesc>"+p.getProjectDescription();
			xml+="</PrjectDesc><PrjectCatog>"+p.getProjectCategory();
			xml+="</PrjectCatog><ProjectComments>";
			ArrayList<String> comnts=p.getComments();
			for (int i=0;i<comnts.size();i++)
			{
				xml+="<comment>"+comnts.get(i) +"</comment>";
			}
			
			xml+="</ProjectComments></Project>";
			///xml+="</Project>";
			
		}
	
			
		return xml;
	}
	
	public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException
	{
		
		PrintWriter out =response.getWriter();
		String projectaction = request.getParameter("projectaction");
		
		if(projectaction==null)
		{
			Project p = new Project();
			
			//  xml produce ....
			String xml=getXML(p,"ProjectNameId");
			out.print(xml);	
			//-----------------------------------
			
			
		//	request.setAttribute("AllProjects", p.viewproject());
			
		//	RequestDispatcher view = request.getRequestDispatcher("allprojects.jsp");
			//view.forward(request, response);
		}
		else if(projectaction.equals("post"))
		{
			String projectName = request.getParameter("projectName");
			paramType.add("String");
			String projectDiscription = request.getParameter("projectDiscription");
			paramType.add("String");
			String category = request.getParameter("category");
			paramType.add("String");
			out.print(projectName+projectDiscription+category);
			 //projectModel post = new projectModel();
			 //post.postproject(projectName,projectDiscription,category);
			Project project = new Project();
			param.add(projectName);
			param.add(projectDiscription);
			param.add(category);
			 //out.print("ControllerProject Disc"+project.getProjectName());
					 
			 project.Post(param,paramType);
			 out.print("Project Posted Successfully");
		}
		else if(projectaction.equals("detailview"))
		{
			
			
			int pid = Integer.parseInt(request.getParameter("param_name"));
			Project p = new Project();
			
		
			// xml for project detail
			p=p.projectDetail(pid);
			p.setComments(p.getAllComments(pid));
			String xml=getXML(p,"projectdetail");
			out.print(xml);
			//-----------------------------------
			
			
			//request.setAttribute("ProjectDetail", p.projectDetail(pid));
			//request.setAttribute("AllComments", p.getAllComments(pid));
			
			//RequestDispatcher view = request.getRequestDispatcher("projectdetail.jsp");
			//view.forward(request, response);
			
		}
		else if(projectaction.equals("comment"))
		{
			String comment = request.getParameter("comment");
			Project project = new Project();
			int pid = Integer.parseInt(request.getParameter("pid"));
			paramType.clear();
			param.clear();
			paramType.add("String");
			paramType.add("Integer");
			param.add(comment);
			param.add(pid);
			if(project.addComment(param,paramType) > 0){
				Project p = new Project();
				p=p.projectDetail(pid);
				p.setComments(p.getAllComments(pid));
				String xml=getXML(p,"projectdetail");
				out.print(xml);
				//request.setAttribute("ProjectDetail", p.projectDetail(pid));
				//request.setAttribute("AllComments", p.getAllComments(pid));
				//RequestDispatcher view = request.getRequestDispatcher("projectdetail.jsp");
				//view.forward(request, response);
			}
				
		}
		 
	}
	
}